using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;

namespace TienTeTuong
{
	/// <summary>
	/// Summary description for DemoFileManager.
	/// </summary>
	public class UploadFileManager : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
		protected System.Web.UI.HtmlControls.HtmlInputFile File1;
		string [] subdirectoryEntries;
		protected System.Web.UI.WebControls.TextBox directory_text;
		protected System.Web.UI.WebControls.Button directory_btn;
		protected System.Web.UI.WebControls.Button upload_btn;
		protected System.Web.UI.WebControls.Button Button1;
		string [] files;
	    private string currentD;


		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
            if (Session["UploadCurrentDir"] == null)
                Session.Add("UploadCurrentDir", "~\\images\\upload\\");
            currentD = Session["UploadCurrentDir"].ToString();
		    
			if(!IsPostBack)
			{
                FillGrid(currentD, 0);
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.directory_btn.Click += new System.EventHandler(this.directory_btn_Click);
            this.upload_btn.Click +=new EventHandler(upload_btn_Click);
			this.Button1.Click += new System.EventHandler(this.Button1_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		public void FillGrid(string category, int val)
		{
			DataTable dt=new DataTable("datatable1");
			DataColumn dc;
			DataRow dr;
			dr=dt.NewRow();
			
			dc= new DataColumn();
			dc.DataType=System.Type.GetType("System.String");
			dc.ColumnName="Name";
			dt.Columns.Add(dc);

			dc= new DataColumn();
			dc.DataType=System.Type.GetType("System.Int32");
			dc.ColumnName="Size";
			dt.Columns.Add(dc);

			dc= new DataColumn();
			dc.DataType=System.Type.GetType("System.DateTime");
			dc.ColumnName="CreationTime";
			dt.Columns.Add(dc);
					
			dc= new DataColumn();
			dc.DataType=System.Type.GetType("System.String");
			dc.ColumnName="Type";
			dt.Columns.Add(dc);
		
			dc= new DataColumn();
			dc.DataType=System.Type.GetType("System.String");
			dc.ColumnName="FileIcon";
			dt.Columns.Add(dc);
			//dir list
			subdirectoryEntries = Directory.GetDirectories(Server.MapPath(category));
            dt.Clear();
            //foreach(string directoryName in subdirectoryEntries)
            //{
				
            //    dr = dt.NewRow();
            //    DirectoryInfo di = new DirectoryInfo(directoryName);
					
            //    dr[0] = di.Name;
            //    dr[1] = 1000; //(dirSize(di) / 1000) + 1;
            //    dr[2] = di.CreationTime;
            //    dr[3] = "D";
            //    dr[4] = "../images/folder.gif";
            //    dt.Rows.Add(dr);
			
            //}

			//files list
			files=Directory.GetFiles(Server.MapPath("~\\images\\upload\\"));
			foreach (string fileName in files)
			{
				dr = dt.NewRow();
					
				FileInfo fi =new FileInfo(fileName);
				dr[0] = fi.Name;
				dr[1] = (fi.Length / 1000 + 1) ;
				dr[2] = fi.CreationTime;
				dr[3] = "F";
                dr[4] = "../images/upload/" + fi.Name;
				dt.Rows.Add(dr);
			
			}
			DataView  dv = new DataView(dt);
			DataGrid1.DataSource= dv;
			DataGrid1.DataBind();
	
		}


		private void directory_btn_Click(object sender, System.EventArgs e)
		{
			string dir_name=directory_text.Text;
            DirectoryInfo df = Directory.CreateDirectory(Server.MapPath("~\\images\\upload\\") + dir_name);

            FillGrid(currentD, 0);
			
		}

		private void upload_btn_Click(object sender, System.EventArgs e)
		{
				
			string StrFileName = File1.PostedFile.FileName.Substring(File1.PostedFile.FileName.LastIndexOf("\\") + 1) ;
			string StrFileType = File1.PostedFile.ContentType ;
			int IntFileSize =File1.PostedFile.ContentLength;
			
				
			if (IntFileSize <=0)
				Response.Write(" <font color='Red' size='2'>Uploading of file " + StrFileName + " failed </font>");
			else
			{
				File1.PostedFile.SaveAs(Server.MapPath("~\\images\\upload\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + StrFileName));
                FillGrid(currentD, 0);
			}
		
		}

		private void Button1_Click(object sender, System.EventArgs e)
		{
			CheckBox cb;
			try
			{
				foreach(DataGridItem gridItem in DataGrid1.Items)
				{
					cb =(CheckBox) gridItem.FindControl("chk");
					
					if(cb.Checked)
						if(gridItem.Cells[1].Text.Equals("F"))
						{
							File.Delete (Server.MapPath("~\\images\\upload\\" + gridItem.Cells[2].Text));
                            FillGrid(currentD, 0);
						}
						else
						{
							Directory.Delete (Server.MapPath("~\\images\\upload\\" + gridItem.Cells[2].Text),true);
                            FillGrid(currentD, 0);
						}
				}
			}
			catch(Exception ex)
			{
				Response.Write(" Exception DELETE BUTTON "+ ex);
			}
		}

        public void link_btn_Click(object sender, CommandEventArgs e)
        {
            if (e.CommandName.Equals("D"))
            {
                currentD += e.CommandArgument.ToString();
                Session["UploadCurrentDir"] = currentD;
                
                FillGrid(currentD, 0);
            }
        }
	}
}
